<?php
error_reporting(E_ALL^E_NOTICE);
session_start();
if($_POST)
{
	$error=chat_login();
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>

<body>
<form  method="post" enctype="multipart/form-data">
<?php echo $error; ?>
Your Full Name:<input type="text" name="username" />6-15位数字，字母，下划线<br />
<input type="submit" value="Chat" />
</form>
</body>
</html>

<?php
function chat_login()
{
	$error="";
	if(!preg_match("/^\w{6,15}$/",$_POST["username"]))
	{
		$error="Username format is error!";
		return $error;
	}
	$users=scandir(dirname(__FILE__)."/chat/");
	array_shift($users);
	array_shift($users);
	foreach ($users as $username)
	{
		if(strcasecmp($username,$_POST["username"])===0)
		{
			$error="Username is exists,please change your username!";
			return $error;
		}
	}
	setcookie("chat_username",$_POST["username"],0);
	header("Location:chat.php");
}
?>